import Error from './Error'
import { IError, IResponse } from '../types/base'

class LogClass {
	/**
	 * 打印错误日志 - 状态码级别
	 *
	 * @param {error}
	 * @memberof LogClass
	 */
	errorLog(error: IError) {
		const { baseURL, url, method, params, data } = error.config
		const Log_Table = {
			URL: `${baseURL}${url}`,
			METHOD: method,
			PARAMS: JSON.stringify(params),
			DATA: JSON.stringify(data),
			ERROR: '异常问题，请联系管理员！',
		}
		if (error?.constructor?.name === 'Cancel') {
			Log_Table.ERROR = '取消请求'
		}
		if (error.response) {
			const { statusText, status } = error.response
			Log_Table.ERROR = statusText || Error.msgMap[status]
		}
		console.table(Log_Table)
	}

	/**
	 * 打印响应日志
	 *
	 * @param {response}
	 * @memberof LogClass
	 */
	responseLog(response: IResponse) {
		const {
			config: { baseURL, url, method, params },
			data,
		} = response
		console.table({
			URL: `${baseURL}${url}`,
			METHOD: method,
			PARAMS: JSON.stringify(params),
			DATA: JSON.stringify(data),
			ERROR: data.code !== 0 ? data.message : '',
			MESSAGE: '请求成功',
		})
	}
}

export default new LogClass()
